﻿//on successfully completed
function OnSuccess(res) {
    $("#MainContainer").empty();
    $("#MainContainer").append(res);
    //scrollwindow();
}

//on error
function OnError(result, status, error) {
    start = result.responseText.search("<title>") + 7;
    end = result.responseText.search("</title>");
    stackStart = result.responseText.search("<b>Stack Trace:</b>");
    error = "<h2 class='error'>Oops error occured!!!</h2>";
    if (start > 0 && end > 0) {
        error += "[" + result.responseText.substring(start, end) + "]</p></p>";
        error += result.responseText.substring(stackStart).replace("#ffffcc", "#ffffff").replace("<pre>", "").replace("</pre>", "").replace("<code>", "").replace("</code>", "");
    }

    $("#MainContainer").empty();
    $("#MainContainer").append(error);

}



//alternate ajax POST method
function AjaxPOST(frm, action, OnSuccess, OnError) {
    $.ajax(
                  {
                      type: "POST",
                      url: action,
                      cache: false,
                      data: frm.serialize(),
                      success: OnSuccess,
                      error: OnError


                  });
}

//ajax GET method
function AjaxGET(action, OnSuccess, OnError) {

    $.ajax(
{
    type: "GET",
    url: action,
    cache: false,
    data: "{}",
    success: OnSuccess,
    error: OnError

});

    return false;
}

function ValidateSearch(action) {

    var strError = "";
    $(".error_message").hide();
    $(".error").hide();

    if(jQuery.trim($("#tbZipCode").val()) == ""
     && jQuery.trim($("#ddlCounty").val()) == ""
     && jQuery.trim($("#ddlDistance").val()) == "0"
     && jQuery.trim($("#ddlSpeciality").val()) == ""
     && jQuery.trim($("#tbLastName").val()) == ""
     && jQuery.trim($("#tbFirstName").val()) == ""
     && jQuery.trim($("#tbOfficeName").val()) == "")
    {
        strError += "<p>* Please select at least one search criteria.</p>";
    }
    if (strError.length > 0) {
        $(".error_message").attr("innerHTML", strError);
        $(".error_message").show();
         return false;
     }
     var strError = "";
    var validZipCode = true;
    if (jQuery.trim($("#tbZipCode").val()) != "" && !CheckRegexNumeric($("#tbZipCode"))) {
        validZipCode = false;
        strError += "<p>* Please enter valid zip code.</p>";
    }
    if (jQuery.trim($("#tbLastName").val()) != "" && !CheckRegexAlphanumeric($("#tbLastName")))
    {
        strError += "<p>* Please enter valid Last Name.</p>";
    }
    if (jQuery.trim($("#tbFirstName").val()) != "" && !CheckRegexAlphanumeric($("#tbFirstName")))
    {
        strError += "<p>* Please enter valid First name.</p>";
    }

    if (validZipCode && jQuery.trim($("#ddlDistance").val()) != "0" && !CheckRegexZip($("#tbZipCode")))
    {
        strError += "<p>* Please enter a valid and complete zipcode from which to measure the distance . </p>";
    }
    
    
    if (strError.length > 0) {
        $(".error_message").attr("innerHTML", strError);
        $(".error_message").show();
         return false;
    }
    else{
    
    $("#hdnMode").attr("value", "Search");
    var frm = $('#frmContent');
    AjaxPOST(frm, action, OnSuccess, OnError);
    return false;
    }

}

function ValidateEmail(action) {

    var strError = "";
    $(".error_message").hide();
    $(".error").hide();



    if (jQuery.trim($("#tbEmailAddress").val()) == "" || !CheckRegexEmail($("#tbEmailAddress"))) {
        strError += "<p>* Please enter valid e-mail address.</p>";
    }
    


    if (strError.length > 0) {
        $(".error_message").attr("innerHTML", strError);
        $(".error_message").show();
        return false;
    }
    else {

        $("#hdnMode").attr("value", "Email");
        var frm = $('#frmContent');
        AjaxPOST(frm, action, OnSuccess, OnError);
        return false;
    }

}

function CheckRegexAlphanumeric(valInput) {
    var reg = new RegExp("^[a-zA-Z0-9]+$");
    if (reg.test(valInput.val()))
        return true;
    else
        return false;
}

///checks the RegExp against the input value
function CheckRegexEmail(valInput) {
    var reg = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    if (reg.test(valInput.val()))
        return true;
    else
        return false;
}


function CheckRegexNumeric(valInput) {
    var reg = new RegExp("^[0-9]+$");
    if (reg.test(valInput.val()))
        return true;
    else
        return false;
}



function CheckRegexZip(valInput) {
     var reg = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);
    //var reg = new RegExp^(\d{5}-\d{(/(4}|\d{5}|\d{9})$|^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$)/);

    if (reg.test(valInput.val()))
        return true;
    else
        return false;
}


function ValidateGMap(action) {


    $("#hdnMode").attr("value", "GMAP");
    var frm = $('#frmContent');
    AjaxPOST(frm, action, OnSuccess, OnError);
    return false;

}
function ValidatePaging(action) {


    $("#hdnMode").attr("value", "DATA");
    var frm = $('#frmContent');
    AjaxPOST(frm, action, OnSuccess, OnError);
    return false;

}

function ValidateAdministratorDetails111(action) {

    var frm = $('#frmContent');
    var strError = "";
    $(".error_message").hide();

    //    if (jQuery.trim($("#txtAdministratorID").val()) != "") {
    //        if (!CheckRegexEmail($("#txtAdministratorID")))
    //            strError += "<p>Please enter a valid e-mail address.</p>";
    //    }

    //    if (jQuery.trim($("#txtPassword").val()) != "") {
    //        if (jQuery.trim($("#txtPassword").val()).length < 6)
    //            strError += "<p>Your password must contain 6 digits.</p>";
    //    }
    //    else strError += "<p>Please enter a password.</p>";

    //    if (jQuery.trim($("#txtConfirmPassword").val()) != "") {
    //        if (jQuery.trim($("#txtPassword").val()) != $("#txtConfirmPassword").val())
    //            strError += "<p>Your password do not match.</p>";
    //    }
    //    else strError += "<p>Please enter a password for confirmation.</p>";

    //    // if (jQuery.trim($("#txtEMailAddress").val()) != "") {
    //    // if (!CheckRegexEmail($("#txtEMailAddress")))
    //    // strError += "<p>Please enter a valid e-mail address.</p>";
    //    // } else strError += "<p>Please enter your e-mail address.</p>";


    if (strError.length > 0) {
        $(".error_message").attr("innerHTML", strError);
        $(".error_message").show();
    }
    else {
        AjaxPOST(frm, action, OnSuccess, OnError);
    }
    return false;
}

function frmSearchPaging(mode, url, frmName, page) {
    $("#hdnStatus").attr("value", mode);
    //$("#" + frmName).attr("action", url);
    //$("#" + frmName).ajaxSubmit(optionsModalAds);

    var frm = $('#frmContent');
    AjaxPOST(frm, url, OnSuccess, OnError);
    return false;
}
function frmSearchCommandArgument(mode, url, frmName, page) {
    $("#hdnMode").attr("value", "DATAA");
    $("#hdnStatus").attr("value", "PAGING");
    $("input[id*='hdnCurrentPage']")[0].value = mode;
    //$("#" + frmName).attr("action", url);
    var frm = $('#frmContent');
    AjaxPOST(frm, url, OnSuccess, OnError);
    return false;
}


